<?php

namespace app\home\controller;

use think\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 服务机构账号控制器
 */
class FuwuManageAccount extends BaseFuwu {

    public function _initialize() {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'home/lang/' . config('default_lang') . '/fuwu_manage_account.lang.php');
    }

    public function edit_phone() {
        if (!request()->isPost()) {
            $this->assign('o2o_fuwu_account_info', $this->o2o_fuwu_account_info);
            /* 设置机构当前菜单 */
            $this->setFuwuCurMenu('fuwu_manage_account');
            /* 设置机构当前栏目 */
            $this->setFuwuCurItem('edit_phone');
            return $this->fetch($this->template_dir . 'edit_phone');
        } else {
            $password = input('post.password');
            $mobile = input('post.mobile');
            if (!$password) {
                ds_json_encode(10001, lang('o2o_fuwu_account_password_require'));
            }
            if (md5($password) != $this->o2o_fuwu_account_info['o2o_fuwu_account_password']) {
                ds_json_encode(10001, lang('o2o_fuwu_account_password_error'));
            }
            $validate_data = array(
                'o2o_fuwu_account_phone' => $mobile,
            );
            $o2o_fuwu_account_validate = validate('o2o_fuwu_account');
            if (!$o2o_fuwu_account_validate->scene('o2o_fuwu_account_edit_phone')->check($validate_data)) {
                ds_json_encode(10001, $o2o_fuwu_account_validate->getError());
            }
            $o2o_fuwu_account_model = model('o2o_fuwu_account');
            $result = $o2o_fuwu_account_model->editO2oFuwuAccount($validate_data, array('o2o_fuwu_account_id' => $this->o2o_fuwu_account_info['o2o_fuwu_account_id']));
            if ($result) {
                ds_json_encode(10000, lang('ds_common_op_succ'));
            } else {
                ds_json_encode(10001, lang('ds_common_op_fail'));
            }
        }
    }

    public function edit_password() {
        if (!request()->isPost()) {
            /* 设置机构当前菜单 */
            $this->setFuwuCurMenu('fuwu_manage_account');
            /* 设置机构当前栏目 */
            $this->setFuwuCurItem('edit_password');
            return $this->fetch($this->template_dir . 'edit_password');
        } else {
            $password1 = input('post.password1');
            $password2 = input('post.password2');

            $verify_code = input('post.verify_code');
            if (!$password1) {
                ds_json_encode(10001, lang('o2o_fuwu_account_password1_require'));
            }
            if (!$password2) {
                ds_json_encode(10001, lang('o2o_fuwu_account_password2_require'));
            }


            if ($password1 != $password2) {
                ds_json_encode(10001, lang('o2o_fuwu_account_password_not_equal'));
            }

            $validate_data = array(
                'verify_code' => $verify_code,
            );
            $verify_code_validate = validate('verify_code');
            if (!$verify_code_validate->scene('verify_code_search')->check($validate_data)) {
                ds_json_encode(10001, $verify_code_validate->getError());
            }

            $validate_data = array(
                'o2o_fuwu_account_password' => $password1,
            );
            $o2o_fuwu_account_validate = validate('o2o_fuwu_account');
            if (!$o2o_fuwu_account_validate->scene('o2o_fuwu_account_edit_password')->check($validate_data)) {
                ds_json_encode(10001, $o2o_fuwu_account_validate->getError());
            }
            $verify_code_model = model('verify_code');
            if (!$verify_code_model->getVerifyCodeInfo(array('verify_code_type' => 3, 'verify_code_user_type' => 4, 'verify_code_user_id' => $this->o2o_fuwu_account_info['o2o_fuwu_account_id'], 'verify_code' => $verify_code, 'verify_code_add_time' => array('>', TIMESTAMP - VERIFY_CODE_INVALIDE_MINUTE * 60)))) {
                ds_json_encode(10001, lang('o2o_fuwu_account_verify_code_error'));
            }
            $o2o_fuwu_account_model = model('o2o_fuwu_account');
            $result = $o2o_fuwu_account_model->editO2oFuwuAccount(array('o2o_fuwu_account_password' => md5($password1),), array('o2o_fuwu_account_id' => $this->o2o_fuwu_account_info['o2o_fuwu_account_id']));
            if ($result) {
                ds_json_encode(10000, lang('ds_common_op_succ'));
            } else {
                ds_json_encode(10001, lang('ds_common_op_fail'));
            }
        }
    }

    public function send_verify_code() {
        $verify_code_type = input('param.verify_code_type');
        if (!config('sms_password')) {
            ds_json_encode(10001, '平台没开启短信找回密码');
        }
        if (!$this->o2o_fuwu_account_info['o2o_fuwu_account_phone']) {
            ds_json_encode(10001, '该用户没有设置手机');
        }
        //验证发送频率
        $verify_code_model = model('verify_code');
        $result = $verify_code_model->isVerifyCodeFrequant($verify_code_type, 4);
        if (!$result['code']) {
            ds_json_encode(10001, $result['msg']);
        }
        $verify_code = $verify_code_model->genVerifyCode($verify_code_type, 4);
        if ($verify_code) {
            if ($verify_code_type == 3) {//修改密码
                $sms_type = 3;
                $mailmt_code = 'authenticate';
            } else {
                $sms_type = 0;
                $mailmt_code = 'authenticate';
            }
            $tpl_info = model('mailtemplates')->getTplInfo(array('mailmt_code' => $mailmt_code));
            try {
                if (!$this->o2o_fuwu_account_info['o2o_fuwu_account_phone']) {
                    ds_json_encode(10001, '手机号错误');
                }

                $param = array();
                $param['send_time'] = date('Y-m-d H:i', TIMESTAMP);
                $param['verify_code'] = $verify_code;
                $param['site_name'] = config('site_name');
                $message = ds_replace_text($tpl_info['mailmt_content'], $param);
                $smslog_param=array(
                    'ali_template_code'=>$tpl_info['ali_template_code'],
                    'ali_template_param'=>$param,
                    'message'=>$message,
                );
                //发送短信
                $result = model('smslog')->sendSms($this->o2o_fuwu_account_info['o2o_fuwu_account_phone'], $smslog_param, $sms_type, $verify_code, $this->o2o_fuwu_account_info['o2o_fuwu_account_id'], $this->o2o_fuwu_account_info['o2o_fuwu_account_name']);

                if (!$result['state']) {
                    exception($result['message']);
                }
                $ip = request()->ip();
                $flag = $verify_code_model->addVerifyCode(array(
                    'verify_code_type' => $verify_code_type,
                    'verify_code' => $verify_code,
                    'verify_code_user_type' => 4,
                    'verify_code_user_id' => $this->o2o_fuwu_account_info['o2o_fuwu_account_id'],
                    'verify_code_user_name' => $this->o2o_fuwu_account_info['o2o_fuwu_account_name'],
                    'verify_code_add_time' => TIMESTAMP,
                    'verify_code_ip' => $ip,
                ));
                if (!$flag) {
                    exception('新增验证码记录失败');
                }
            } catch (Exception $e) {
                ds_json_encode(10001, $e->getMessage());
            }
        } else {
            ds_json_encode(10001, '请重试');
        }
        ds_json_encode(10000, lang('ds_common_op_succ'));
    }

    /**
     *    栏目菜单
     */
    function getFuwuItemList() {
        $item_list = array(
            array(
                'name' => 'edit_phone',
                'text' => '修改手机',
                'url' => url('FuwuManageAccount/edit_phone'),
            ),
            array(
                'name' => 'edit_password',
                'text' => '修改密码',
                'url' => url('FuwuManageAccount/edit_password'),
            ),
        );
        return $item_list;
    }

}
